package com.ruoyi.system.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.SysVip;
import com.ruoyi.system.domain.WxCheckoutBody;
import com.ruoyi.system.mapper.SysVipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WxRecordsMapper;
import com.ruoyi.system.domain.WxRecords;
import com.ruoyi.system.service.IWxRecordsService;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * 游戏记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@Service
public class WxRecordsServiceImpl implements IWxRecordsService 
{
    @Autowired
    private WxRecordsMapper wxRecordsMapper;

    @Autowired
    private TransactionTemplate transactionTemplate;  // Spring事务模板

    @Autowired
    private SysVipMapper sysVipMapper;

    /**
     * 查询游戏记录
     * 
     * @param id 游戏记录主键
     * @return 游戏记录
     */
    @Override
    public WxRecords selectWxRecordsById(Long id)
    {
        return wxRecordsMapper.selectWxRecordsById(id);
    }

    /**
     * 查询游戏记录列表
     * 
     * @param wxRecords 游戏记录
     * @return 游戏记录
     */
    @Override
    public List<WxRecords> selectWxRecordsList(WxRecords wxRecords)
    {
        return wxRecordsMapper.selectWxRecordsList(wxRecords);
    }

    /**
     * 新增游戏记录
     * 
     * @param wxRecords 游戏记录
     * @return 结果
     */
    @Override
    public int insertWxRecords(WxRecords wxRecords)
    {
        return wxRecordsMapper.insertWxRecords(wxRecords);
    }

    /**
     * 修改游戏记录
     * 
     * @param wxRecords 游戏记录
     * @return 结果
     */
    @Override
    public int updateWxRecords(WxRecords wxRecords)
    {
        return wxRecordsMapper.updateWxRecords(wxRecords);
    }

    /**
     * 批量删除游戏记录
     * 
     * @param ids 需要删除的游戏记录主键
     * @return 结果
     */
    @Override
    public int deleteWxRecordsByIds(Long[] ids)
    {
        return wxRecordsMapper.deleteWxRecordsByIds(ids);
    }

    /**
     * 删除游戏记录信息
     * 
     * @param id 游戏记录主键
     * @return 结果
     */
    @Override
    public int deleteWxRecordsById(Long id)
    {
        return wxRecordsMapper.deleteWxRecordsById(id);
    }

    @Override
    public int startGame(WxRecords record) {
        record.setStartTime(new Date());
        return wxRecordsMapper.insertWxRecords(record);
    }

    @Override
    public int endGame(WxRecords record) {
        record.setEndTime(new Date());
        return wxRecordsMapper.endGame(record);
    }

    @Override
    public WxRecords getActiveRecord(WxRecords record) {
        return wxRecordsMapper.getActiveRecord(record);
    }

    /*@Override
    public int saveUser(WxUser user) {
        WxUser existing = userMapper.selectByOpenid(user.getOpenid());
        if (existing == null) {
            return userMapper.insert(user);
        }
        return 0;
    }*/

    /**
     * 游戏记录结账
     *
     * @param body 结账实体
     * @return 游戏记录
     */
    public int checkout(WxCheckoutBody body){
        /** 更新VIP余额 + 批量更新records（事务管理） */
        return transactionTemplate.execute(status -> {
            // 1. 更新VIP余额
            SysVip currentVip = new SysVip();
            currentVip.setVipId(body.getVipId());
            currentVip.setBalance(body.getBalance());
            int vipUpdateRows = sysVipMapper.updateSysVip(currentVip);
            if (vipUpdateRows == 0) {
                status.setRollbackOnly(); // 标记回滚（可选）
                return 0; // 返回失败
            }

            // 2. 批量更新records
            int recordsUpdated = wxRecordsMapper.updateRecordsStatus(body.getRecordIdList());
            if (recordsUpdated != body.getRecordIdList().size()) {
                return 0; // 返回失败
            }

            return 1; // 返回成功
        });
    }

    /**
     * 游戏记录结账
     *
     * @param body 结账实体
     * @return 游戏记录
     */
    public int checkoutWhitoutVip(WxCheckoutBody body){
        /** 批量更新records（事务管理） */
        return transactionTemplate.execute(status -> {
              // 2. 批量更新records
            int recordsUpdated = wxRecordsMapper.updateRecordsStatus(body.getRecordIdList());
            if (recordsUpdated != body.getRecordIdList().size()) {
                return 0; // 返回失败
            }
            return 1; // 返回成功
        });
    }

    /**
     * 手机端单独结账
     *
     * @param record 游戏记录
     * @return 游戏记录
     */
    public int checkoutOne(WxRecords record){
        return wxRecordsMapper.checkoutOne(record);
    }
}
